package com.xgd.phylw.controller;

import com.xgd.phylw.bean.Orderd;
import com.xgd.phylw.bean.Seat;
import com.xgd.phylw.service.OrderService;
import com.xgd.phylw.service.SeatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
public class BuyTicketController {
    @Autowired
    private SeatService seatService;
    @Autowired
    private OrderService orderService;
    @RequestMapping("BuyTicket")
    @ResponseBody
    public List<Object> BuyTicket(Integer[] oldRow, Integer[] oldColumn, Double total, Integer scheduleId, Integer hallId, String memberName){
        List<Object> dataList = new ArrayList<Object>();
        List<Orderd> orderdList = setOrderdBean(oldRow,oldColumn,scheduleId,memberName);
        int insert = orderService.insertByOrder(orderdList);
        List<Seat> seatList = setSeatRowAndColumn(oldRow, oldColumn, hallId, scheduleId);
        int seatByList = seatService.insertSeatByList(seatList);
        List<Seat> selectByScheduleId = seatService.selectByScheduleId(scheduleId);
        List<Integer> row = new ArrayList<Integer>();
        List<Integer> column = new ArrayList<Integer>();
        if(selectByScheduleId.size()>0){
            for (Seat seat : selectByScheduleId) {
                row.add(seat.getSetRow());
                column.add(seat.getSetColumn());
            }
        }
        dataList.add(row);
        dataList.add(column);
        return dataList;
    }
//    将前台传送的数据封装成Seat对象集合
    public List<Seat> setSeatRowAndColumn(Integer[] oldRow, Integer[] oldColumn, Integer hallId, Integer scheduleId){
        List<Seat> seatList = new ArrayList<Seat>();
        for(int i=0;i< oldRow.length;i++){
            Seat seat = new Seat();
            seat.setScheduleId(scheduleId);
            seat.setHallId(hallId);
            seat.setSetRow(oldRow[i]);
            seat.setSetColumn(oldColumn[i]);
            seat.setSetIssell(1);
            seatList.add(seat);
        }
        return seatList;
    }
    //    将前台传送的数据封装成Order对象集合
    public List<Orderd> setOrderdBean(Integer[] oldRow, Integer[] oldColumn,Integer scheduleId, String memberName){
        List<Orderd> orderdList = new ArrayList<Orderd>();
        for(int i=0;i< oldRow.length;i++){
            Orderd orderd = new Orderd();
            orderd.setScheduleId(scheduleId);
            orderd.setMemberName(memberName);
            orderd.setSetRow(oldRow[i]);
            orderd.setSetColumn(oldColumn[i]);
            orderdList.add(orderd);
        }
        return orderdList;
    }
}
